Shader "Custom/RendererTest"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        [Toggle(_True)] _IsCircle ("IsCircle", Float) = 1
        
        // 水流噪声选项
        [Toggle(_True)] _IsFlow ("IsFlow", Float) = 1
        _NoiseTex ("NoiseTex", 2D) = "white" {} 
        _NoiseStrength ("NoiseStrength", Vector) = (0, 0, 0, 0)
        _NoiseSpeed ("NoiseSpeed", Vector) = (0, 0, 0, 0)
    }
    SubShader
    {
        Tags { "RenderType"="Transparent" "Queue"="Transparent" "IgnoreProjector"="True" }
        LOD 100

        Pass
        {
            Name "RenderInstanced"
            
            Blend SrcAlpha OneMinusSrcAlpha
            ZTest LEqual
            ZWrite Off
            Cull Off
            
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_instancing
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
                UNITY_VERTEX_INPUT_INSTANCE_ID
                float4 texcoord : TEXCOORD1;
                float4 color : COLOR; // 顶点颜色
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
                float2 uv : TEXCOORD0;
                UNITY_VERTEX_INPUT_INSTANCE_ID
                float2 uv_Noise : TEXCOORD1;
                float4 color : COLOR;
            };

            // Instance属性
            // UNITY_INSTANCING_BUFFER_START(Props)
            //     UNITY_DEFINE_INSTANCED_PROP(float4x4, _TransformMatrixData)
            // UNITY_INSTANCING_BUFFER_END(Props)

            sampler2D _MainTex;
            float4 _MainTex_ST;
            bool _IsCircle;

            // 水波
            sampler2D _NoiseTex;
            fixed4 _NoiseTex_ST;
            fixed2 _NoiseStrength;
            fixed2 _NoiseSpeed;

            v2f vert (appdata v)
            {
                v2f o;
                UNITY_SETUP_INSTANCE_ID(v);
                UNITY_TRANSFER_INSTANCE_ID(v, o);
                // 获取instance数据
                // float4x4 transformMatrixData = UNITY_ACCESS_INSTANCED_PROP(Props, _TransformMatrixData);
                // float3 worldPos = mul(transformMatrixData, v.vertex);
                // 变换到裁剪空间
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                o.color = v.color;
                // 水流速度
                _NoiseTex_ST.zw += _Time * _NoiseSpeed;
                o.uv_Noise = TRANSFORM_TEX(v.texcoord, _NoiseTex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                // 是否裁切为圆形
                if (_IsCircle)
                { 
                    float dist = distance(i.uv, float2(0.5, 0.5));
                    if (dist > 0.5) discard;
                }
                UNITY_SETUP_INSTANCE_ID(i);
                // 水流扰动
                float2 offset;
                {
                    float r = tex2D(_NoiseTex, i.uv_Noise).r;
                    offset = float2(r, r);
                    // offset -= 0.1;
                    offset *= _NoiseStrength; 
                }
                fixed4 col = tex2D(_MainTex, i.uv + offset);
                
                // 保持原始alpha值，只混合RGB颜色
                if (i.color.r > 0.9 && i.color.g > 0.9 && i.color.b > 0.9)
                {
                    
                }else
                {
                    fixed3 colorBlend = lerp(col.rgb, i.color.rgb, 0.9);
                    col.rgb = colorBlend;
                }

                // if (col.a < 0.1)
                // {
                //     col.rgba = fixed4(0.9, 0.9, 0.9, 1);
                // }

                return col;
            }
            ENDCG
        }
    }
    Fallback "Diffuse"
}
